﻿@{
    Layout = "~/Views/Shared/_Layout_lay_admin.cshtml";
}
<style>
    body {
        background-color: #ffffff;
    }

    .custom-tree-item-clicked {
        color: #ff5722
    }
</style>


<div class="layui-form layuimini-form">

    <div class="layui-form-item" style="display:none">
        <label class="layui-form-label ">代码</label>
        <div class="layui-input-block">
            <input type="text" name="ProjectId" id="ProjectId" class="layui-input">
            <input type="text" name="CompanyType" value="Customer" id="CompanyType" class="layui-input">
            <input type="text" name="User_Id" value="" id="User_Id" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">@ViewBag.SelectUser</label>
        <div class="layui-input-block" style=" border: solid 1px #27AAA5;min-height:400px;margin-right:50px;">
            <div id="test9" class="demo-tree demo-tree-box" style="width: 80%; height: 100%; "></div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label ">@ViewBag.Selected</label>
        <div class="layui-input-block">
            <input type="text" readonly="readonly" id="SelectedUser" name="SelectedUser" value="" autocomplete="off" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>

<script>
    var tree;

    var nodeName = "";

    var companytype = GetQueryString("companytype");

    var treeloadload = function () {

        $.ajax({
            type: "GET",
            url: '/Base_Department/SearchByCompany?keyword=' + companytype,
            dataType: 'json',
            success: function (_data) {
                //debugger
                //, url: '/Base_Role/Search'
                //开启节点操作图标
                tree.render({
                    elem: '#test9'
                    , data: _data
                    //, edit: ['add', 'del'] //操作节点的图标
                    , click: function (obj) {
                        //layer.msg(obj.data.id + "," + obj.data.title + "," + obj.data.attrName);
                        var isuser = obj.data.attrName;

                        if (isuser == "U") {
                            $("#User_Id").val(obj.data.id);
                            $("#SelectedUser").val(obj.data.title);
                        } else {
                            $("#User_Id").val("");
                            $("#SelectedUser").val("");
                        }


                    },
                    text: {
                        defaultNodeName: nodeName //节点默认名称
                        , none: '' //数据为空时的提示文本
                    },
                    limitNodeAddLevel: 0
                    , customOperate: true
                    , operate: function (obj, callback) {

                        //layer.msg(id);

                    }
                });
            }
        });

        var index0pen;

    }



    layui.use(['tree', 'form'], function () {
        window.tree = layui.tree;
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;


        treeloadload();


        $("#ProjectId").val(GetQueryString("ProjectId"));

        //监听提交
        form.on('submit(saveBtn)', function (data) {

            if ($("#User_Id").val() == "") {
                layer.alert('@ViewBag.Department_UserNotNull');
                return;
            }

            $.ajax({
                url: '/Base_Department/BindUserDepartment',
                type: 'post',
                dataType: 'text',
                data: data.field,
                success: function (resp) {
                    var json = eval('(' + resp + ')');
                    var index = layer.alert(json.Message, function () {
                        // 关闭弹出层
                        layer.close(index);
                        parent.reloadTable();
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                    //layer.alert(json.Message);
                    //reloadTable();
                },
                error: function (resp) {
                    layer.alert("对不起出错了");
                }
            });

            return false;
        });

    });
</script>
